// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import  Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './components/App'
import router from './router'
import store_ from './store'
import Print from 'vue-print-nb'
import echarts from 'echarts'
import global_ from  './Global.js'
import axios from 'axios'
import VCharts from 'v-charts'
// 百度地图组件
import BaiduMap from 'vue-baidu-map'
//echarts组件引入
import ECharts from 'vue-echarts/components/ECharts'
import 'echarts/lib/chart/line'

Vue.component('chart', ECharts)
/*Vue.prototype.bus = new Vue();*/

Vue.prototype.$http = axios;
Vue.prototype.$echarts = echarts;
Vue.prototype.store = store_;
Vue.prototype.$BASE_URL = global_.BASE_URL;
Vue.prototype.$USER_INFO = global_.USER_INFO;

Vue.config.productionTip = false;

Vue.use(VCharts)
Vue.use(ElementUI);
Vue.use(Print);
Vue.use(BaiduMap, {
  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  ak: 'pkWX8eWsquKgIrHnDvYwVGVW9lg501yD'
})

router.beforeEach((to, from, next) => {
  next()
  // if(to.path === '/login')  {
  //   next()
  // }  // 如果即将进入登录路由，则直接放行
  // else {//进入的不是登录路由
  //   if(!sessionStorage.getItem('accessToken')){
  //     next({ path: '/login' })
  //   }else{
  //     next()
  //   }
  // }
});

axios.interceptors.request.use(
  config => {
    //判断是否存在token，即判断用户是否登录，如果存在的话，则每个http header都加上ticket
    if (sessionStorage.getItem("token")) {
      config.headers['token'] = sessionStorage.getItem("token");
    }
    return config;
  },
  error => {
    return Promise.reject(error.response);
  });

axios.interceptors.response.use(
  response => {
    if(response.data.code == 199){
      router.replace({
        path: '/login',
      })
    }
    return response;
  },
  error => {
    console.log(error.response);
    return Promise.reject(error.response.data)   // 返回接口返回的错误信息
  });

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render: h => h(App)
})
